package com.coder4j.service;

import com.coder4j.entity.SControlDetailEntity;
import com.coder4j.entity.STaskFlowDetailEntity;
import com.coder4j.mapper.SControlDetailMapper;
import com.coder4j.utils.NumberUtils;
import com.coder4j.utils.StringUtils;
import com.coder4j.vo.SControlDetailQuery;
import com.coder4j.vo.SControlDetailVO;
import com.coder4j.starter.web.BaseServiceImpl;
import com.coder4j.vo.STaskFlowDetailQuery;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 任务流程表
 * @author keller
 * @date 2025-03-18 16:22:25
 */
@Service
@Slf4j
public class SControlDetailService extends BaseServiceImpl<SControlDetailMapper,SControlDetailEntity,SControlDetailVO> {

    @Resource
    private SControlDetailMapper mapper;

    @Lazy
    @Resource
    private STaskFlowDetailService flowDetailService;

    @Override
    public SControlDetailVO entity2Vo(SControlDetailEntity entity) {
        SControlDetailVO vo = new SControlDetailVO();
        BeanUtils.copyProperties(entity,vo);
        if(StringUtils.notEmpty(vo.getTaskDetailIds())){
            List<Integer> idList = new ArrayList<>();
            for (String str : vo.getTaskDetailIds().split(",")) {
                idList.add(NumberUtils.parseInteger(str));
            }

            List<String> nameList = flowDetailService.baseQueryList(new STaskFlowDetailQuery())
                    .stream()
                    .filter(item -> idList.contains(item.getId()))
                    .map(STaskFlowDetailEntity::getNodeName)
                    .collect(Collectors.toList());
            vo.setTaskDetailNames(String.join(",",nameList));
        }

        return vo;
    }

    @Override
    public SControlDetailEntity vo2Entity(SControlDetailVO vo) {
        SControlDetailEntity entity = new SControlDetailEntity();
        BeanUtils.copyProperties(vo, entity);
        return entity;
    }
}
